#pragma once
extern "C" {
#include <libavcodec/avcodec.h>
}
#include <windows.h>
#include <d3d9.h>
#include <dxva2api.h>
#include <iostream>
typedef struct dxva2_mode {
	const GUID     *guid;
	enum AVCodecID codec;
} dxva2_mode;
class D3DRender
{
public:
	D3DRender(HWND hwnd, int width, int height,AVCodecContext* ctx = nullptr);
	void render(LPDIRECT3DSURFACE9 surface);
	~D3DRender();
private:
	HWND hwnd_;
	IDirect3D9* d3d9_;
	IDirect3DDevice9* d3d_dev_;
	IDirect3DDeviceManager9* d3d_dev_mgr_;
	HANDLE h_dev_;
	D3DPRESENT_PARAMETERS d3d_prepar_;
	LPDIRECT3DSURFACE9* d3d_surface_;

	int surface_nums_ = 1;
	IDirect3DSurface9* back_buffer_;
	IDirectXVideoDecoderService* decoder_service_;
	IDirectXVideoDecoder* decoder_;
	DXVA2_ConfigPictureDecode dxva2_cfg_;
	GUID decoder_guid_;
	AVCodecContext* codec_ctx_;
	D3DFORMAT d3d_fmt_;

	BOOL GetDxva2FormatAndGuid(AVCodecContext *pCodecCtx, GUID & guid, D3DFORMAT & fmt);
	void GetDecoderCfg(AVCodecContext *pCodecCtx, const GUID *pGuid, const DXVA2_VideoDesc *pDesc, DXVA2_ConfigPictureDecode *pCfg);
};

